<?php

namespace App\Http\Controllers\Home;

use App\Model\Department;
use App\Model\Staff;
use App\Model\Staff_task;
use App\Model\Store;
use App\Model\Store_station;
use App\Model\Task;
use App\Model\Users;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use League\Flysystem\Exception;
//人员配置
class StaffController extends CommonController
{
    //
	protected $model;
	protected $Department;
	protected $Store_station;
	protected $Store;
	protected $Task;
	protected $Staff_task;
	protected $users;

	public function __construct()
	{
		parent::__construct();

		$this->model=new Staff();
		$this->Department=new Department();
		$this->Store_station=new Store_station();
		$this->users=new Users();
		$this->Store=new Store();
		$this->Task=new Task();
		$this->Staff_task=new Staff_task();
	}


//	人员列表
	public function listStaff(Request $request){
		$where['company_id']=$this->company_id;
		try{
			$where['name']=isset($request->name)?$request->name:null;
			$where['start_time']=isset($request->start_time)?$request->start_time:null;
			$where['end_time']=isset($request->end_time)?$request->end_time:null;
			$where['status']=isset($request->status)?$request->status:null;
			$where['phone']=isset($request->phone)?$request->phone:null;
			$where['department']=isset($request->department)?$request->department:null;
			$where['status_']=isset($request->status_)?$request->status_:null;
//			$data= $this->model->lists($where);
			$data= $this->users->lists($where);
		}catch (Exception $exception){
			$message=$exception->getMessage();
		}

		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}


	}
//	新增人员
//	public function addStaff(Request $request){
//		DB::beginTransaction();
//		try{
//			$data=$request->only($this->model->getFillable());
//			$usersData['realname']=$request->name;
//			$usersData['phone']=$request->phone;
//			$usersData['password']=bcrypt('123456');
//			$usersData['company_id']=$this->company_id;
////			新增账号
//			$uid=$this->users->createUser($usersData);
//			$data['company_id']=$this->company_id;
//			$data['uid']=$uid;
//			$result=$this->model->create($data);
//			if($request->station){
////				岗位不为空添加任务
//				$staff_id=$result->id;
//				$arr['store_id']=$this->Store->department($request->department);
//				$arr['type']=2;
//				$arr['status']=1;
//				$arr['store_station_id']=$request->station;
//				$arr['store_id']=$this->store_id;
//				$this->addTask($arr,$staff_id);
//			}
//
//			DB::commit();
//		}catch(\Exception $exception){
//			$message=$exception->getMessage();
//			DB::rollBack();
//		}
//		if(!isset($message)){
//			return $this->stored($result);
//		}else{
//			return $this->failed($message);
//		}
//	}


//  新增员工
	public function addStaff(Request $request){
		$this->validate($request,[
			'name'=>'required|unique:Staff|max:255',
			'contract_number'=>'Numeric',
			'type'=>'required|Integer',
			'phone'=>'required|unique:Staff|Numeric',
			'address'=>'String|max:255',
			'department'=>'Integer',
			'station'=>'Integer',
			'card'=>'Numeric',
			'opening_bank'=>'String',
			'status'=>'Numeric',
			'job'=>'required|Integer',
		]);
		DB::beginTransaction();
		try{
			$data=$request->only($this->model->getFillable());
			$data['avator']='/storage/avatar/f0pHQ2LgxZJOUSDsI7WsG7B6kxkckCc64XEWf47V.jpeg';
			$usersData=$data;
			$usersData['realname']=$request->name;
			$usersData['phone']=$request->phone;
			$usersData['password']=bcrypt('123456');
			$usersData['avator']='/storage/avatar/f0pHQ2LgxZJOUSDsI7WsG7B6kxkckCc64XEWf47V.jpeg';
			$usersData['company_id']=$this->company_id;
			$storeData=$this->Store->department($request->department);
			if(count($storeData)){
				$usersData['is_manage']=1;
			}else{
				$usersData['is_manage']=2;
			}
//			新增账号
			$uid=$this->users->createUser($usersData);
			$data['company_id']=$this->company_id;
			$data['uid']=$uid;
			if($request->station){
//				岗位不为空添加任务
				if(count($storeData)){
					$store_id=$storeData->id;
					$result=$this->model->create($data);
					$staff_id=$result->id;
					$arr['store_id']=$store_id;
					$arr['type']=2;
					$arr['status']=1;
					$arr['store_station_id']=$request->station;
					$this->addTask($arr,$staff_id);
				}
			}

			DB::commit();
		}catch(\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->stored($uid);
		}else{
			return $this->failed($message);
		}
	}
//	编辑员工
	public function editStaff(Request $request){

		try{
			$result=$this->users->where(['id'=>$request->id])->first();
			$result['name']=$result['realname'];

		}catch(\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($result);
		}else{
			return $this->failed($message);
		}
	}



//	编辑保存员工
	public function saveStaff(Request $request){
		$this->validate($request,[
			'name'=>'required|unique:Staff|max:255',
			'contract_number'=>'Numeric',
			'type'=>'required|Integer',
			'phone'=>'required|unique:Staff|Numeric',
			'address'=>'String|max:255',
			'department'=>'Integer',
			'station'=>'Integer',
			'card'=>'Numeric',
			'opening_bank'=>'String',
			'status'=>'Numeric',
			'job'=>'required|Integer',
		]);
		try{
			$data=$request->only($this->model->getFillable());
			$result=$this->users->where(['id'=>$request->id])->update($data);
		}catch(\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->updated($result);
		}else{
			return  $this->failed($message);
		}
	}


//	给配置岗位的员工添加任务绑定任务
	public function addTask($arr,$staff_id){
		DB::beginTransaction();
		try{
			$result=$this->Task->create($arr);
			$data['task_id']=$result->id;
			$data['staff_id']=$staff_id;
			$data['store_station_id']=$arr['store_station_id'];
			$data['status']=3;
			$data['store_id']=$arr['store_id'];
			$res=$this->Staff_task->create($data);
			DB::commit();
		}catch(Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
	}

}
